package com.tms.quartz.service.impl;

import com.tms.quartz.service.JobService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 夹具job服务实现类
 *
 * @author jackie
 * @date 2020年6月30日
 */
@Service
public class JobServiceImpl implements JobService {

    @Autowired
    Scheduler scheduler;

    /**
     * 添加一个job
     *
     * @param clazz    对应的job类
     * @param jobName  job名称
     * @param jobGroup job分组
     * @param param    job参数
     * @return 成功返回建立的job
     * @throws SchedulerException scheduler异常
     */
    @Override
    public JobDetail addJob(Class clazz, String jobName, String jobGroup, Map<String, String> param) throws SchedulerException {
        JobKey jobKey = new JobKey(jobName, jobGroup);
        if (!scheduler.checkExists(jobKey)) {
            //将job加入到jobDetail中
            JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(jobName, jobGroup).build();
            if (param != null) {
                jobDetail.getJobDataMap().putAll(param);
            }
            return jobDetail;
        }
        return null;
    }

    /**
     * 暂停job
     *
     * @param jobName  job名称
     * @param jobGroup job分组
     * @return 成功返回true，失败返回false
     * @throws SchedulerException scheduler异常
     */
    @Override
    public boolean pauseJob(String jobName, String jobGroup) throws SchedulerException {
        JobKey jobKey = new JobKey(jobName, jobGroup);
        if (scheduler.checkExists(jobKey)) {
            scheduler.pauseJob(jobKey);
            return true;
        }
        return false;
    }

    /**
     * 恢复job
     *
     * @param jobName  job名称
     * @param jobGroup job分组
     * @return 成功返回true，失败返回false
     * @throws SchedulerException scheduler异常
     */
    @Override
    public boolean resumeJob(String jobName, String jobGroup) throws SchedulerException {
        JobKey jobKey = new JobKey(jobName, jobGroup);
        if (scheduler.checkExists(jobKey)) {
            scheduler.resumeJob(jobKey);
            return true;
        }
        return false;
    }

    /**
     * 删除job
     *
     * @param jobName  job名称
     * @param jobGroup job分组
     * @return 成功返回true，失败返回false
     * @throws SchedulerException scheduler异常
     */
    @Override
    public boolean removeJob(String jobName, String jobGroup) throws SchedulerException {
        JobKey jobKey = new JobKey(jobName, jobGroup);
        if (scheduler.checkExists(jobKey)) {
            scheduler.pauseJob(jobKey);
            scheduler.deleteJob(jobKey);
            return true;
        }
        return false;
    }

    /**
     * 返回job的triggers
     *
     * @param jobName  job名称
     * @param jobGroup job分组
     * @return job对应的triggers
     * @throws SchedulerException scheduler异常
     */
    @Override
    public List<Trigger> viewTriggersJob(String jobName, String jobGroup) throws SchedulerException {
        List<Trigger> triggers;
        JobKey jobKey = new JobKey(jobName, jobGroup);
        triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);
        return triggers;
    }

    /**
     * 启动所有定时任务
     */
    @Override
    public void startAllJobs() throws RuntimeException {
        try {
            scheduler.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 关闭所有定时任务
     */
    @Override
    public void shutdownAllJobs() {
        try {
            if (!scheduler.isShutdown()) {
                scheduler.shutdown();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
